﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using Telerik.Web.UI;
using System.Collections.Generic;
using KPIS.GERP.GIMS.DAL;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.APP.APP_UI.Add_Form
{
    public partial class ICM_Asset_Warranty : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        D_AssetWarranty d_warranty = new D_AssetWarranty();
        M_AssetWarranty m_warranty = new M_AssetWarranty();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hWarranty_seq.Text = Request.QueryString["warranty_seq"].ToString();
                hAssetStatus.Text = Request.QueryString["StatusName"].ToString();
                pAssetCode.Text = Request.QueryString["asset_code"].ToString();
                if (hAssetStatus.Text == "Add")
                {
                    ClearObject();
                }
                else if (hAssetStatus.Text == "Edit")
                {
                    LoadData();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ChkValidate())
            {
                try
                {
                    m_warranty.asset_code = pAssetCode.Text;
                    m_warranty.warranty_desc = txtWarrantyDesc.Text;
                    m_warranty.warranty_remark = txtWarrantyRemark.Text;
                    m_warranty.warranty_start_date = Convert.ToDateTime(txtReceiveDate.TextWithLiterals);
                    m_warranty.warranty_end_date = cn.ConvertNullDateTime(txtEndDate.TextWithLiterals);
                    m_warranty.record_status = "U";
                    if (hAssetStatus.Text == "Add")
                    {
                        m_warranty.created_by = cn.UserID;
                        m_warranty.created_when = DateTime.Now;
                        d_warranty.Insert_Asset_Warranty(m_warranty);
                        RadAjaxManager1.Alert("เพิ่มข้อมูลเรียบร้อยแล้ว");
                    }
                    else if (hAssetStatus.Text == "Edit")
                    {
                        m_warranty.warranty_seq = Convert.ToInt32(hWarranty_seq.Text);
                        m_warranty.updated_by = cn.UserID;
                        m_warranty.updated_when = DateTime.Now;
                        d_warranty.Update_Asset_Warranty(m_warranty);
                        RadAjaxManager1.Alert("แก้ไขข้อมูลเรียบร้อยแล้ว");
                    }
                }
                catch (Exception ex)
                {
                    RadAjaxManager1.Alert("เกิดข้อผิดพลาดระหว่างทำการบันทึกโปรดตรวจสอบ");
                    throw (ex);
                }
                finally
                {
                    RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
                }
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if(hAssetStatus.Text =="Add")
            ClearObject();
            else if (hAssetStatus.Text =="Edit")
            RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
        }
        private void ClearObject()
        {
            txtReceiveDate.Text = "";
            txtEndDate.Text = "";
            txtWarrantyDesc.Text = "";
            txtWarrantyRemark.Text = "";
        }
        private void LoadData()
        {
            M_AssetWarranty info = d_warranty.GetListFillBySeq(hWarranty_seq.Text);
            txtReceiveDate.Text = cn.ConvertStrToStrDate(info.warranty_start_date.ToString());
            txtEndDate.Text = cn.ConvertStrToStrDate(info.warranty_end_date.ToString());
            txtWarrantyDesc.Text = info.warranty_desc.ToString();
            txtWarrantyRemark.Text = info.warranty_remark.ToString();
            txtReceiveDate.Text = cn.ConvertStrToStrDate(info.warranty_start_date.ToString());
            txtEndDate.Text = cn.ConvertStrToStrDate(info.warranty_end_date.ToString());
        }
        private bool ChkValidate()
        {
            bool pFlag = true;
            if (txtReceiveDate.Text == "")
            {
                pFlag = false;
                txtReceiveDate.Focus();
                RadAjaxManager1.Alert("กรุณาเลือกวันที่เริ่มประกันด้วย");
            }
            else if (txtWarrantyDesc.Text == "")
            {
                pFlag = false;
                txtWarrantyDesc.Focus();
                RadAjaxManager1.Alert("กรอกรายละเอียดเกี่ยวกับการประกันพัสดุด้วย");
            }
            return pFlag;
        }
    }
}
